home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60src.lha / Vim / vim60 / src / diff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-19  |  48.3 KB  |  2,032 lines

  1. /* vim:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * diff.c: code for diff'ing two or three buffers.
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. #if defined(FEAT_DIFF) || defined(PROTO)
  17.  
  18. #define DB_COUNT 4    /* up to four buffers can be diff'ed */
  19.  
  20. /*
  21.  * Each diffblock defines where a block of lines starts in each of the buffers
  22.  * and how many lines it occupies in that buffer.  When the lines are missing
  23.  * in the buffer the df_count[] is zero.  This is all counted in
  24.  * buffer lines.
  25.  * There is always at least one unchanged line in between the diffs.
  26.  * Otherwise it would have been included in the diff above or below it.
  27.  * df_lnum[] + df_count[] is the lnum below the change.  When in one buffer
  28.  * lines have been inserted, in the other buffer df_lnum[] is the line below
  29.  * the insertion and df_count[] is zero.  When appending lines at the end of
  30.  * the buffer, df_lnum[] is one beyond the end!
  31.  * This is using a linked list, because the number of differences is expected
  32.  * to be reasonable small.  The list is sorted on lnum.
  33.  */
  34. typedef struct diffblock diff_T;
  35. struct diffblock
  36. {
  37.     diff_T    *df_next;
  38.     linenr_T    df_lnum[DB_COUNT];    /* line number in buffer */
  39.     linenr_T    df_count[DB_COUNT];    /* nr of inserted/changed lines */
  40. };
  41.  
  42. static diff_T    *first_diff = NULL;
  43.  
  44. static buf_T    *(diffbuf[DB_COUNT]);
  45.  
  46. static int    diff_invalid = TRUE;    /* list of diffs is outdated */
  47.  
  48. static int    diff_busy = FALSE;    /* ex_diffgetput() is busy */
  49.  
  50. /* flags obtained from the 'diffopt' option */
  51. #define DIFF_FILLER    1    /* display filler lines */
  52. #define DIFF_ICASE    2    /* ignore case */
  53. #define DIFF_IWHITE    4    /* ignore change in white space */
  54. static int    diff_flags = DIFF_FILLER;
  55.  
  56. #define LBUFLEN 50        /* length of line in diff file */
  57.  
  58. static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
  59.                     doesn't work, MAYBE when not checked yet */
  60.  
  61. static int diff_buf_idx __ARGS((buf_T *buf));
  62. static void diff_check_unchanged __ARGS((diff_T *dp));
  63. static int diff_check_sanity __ARGS((diff_T *dp));
  64. static void diff_redraw __ARGS((int dofold));
  65. static void diff_file __ARGS((char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff));
  66. static void diff_clear __ARGS((void));
  67. static int diff_equal_entry __ARGS((diff_T *dp, int idx1, int idx2));
  68. static int diff_cmp __ARGS((char_u *s1, char_u *s2));
  69. #ifdef FEAT_FOLDING
  70. static void diff_fold_update __ARGS((diff_T *dp, int skip_idx));
  71. #endif
  72. static void diff_read __ARGS((int idx_orig, int idx_new, char_u *fname));
  73. static void diff_copy_entry __ARGS((diff_T *dprev, diff_T *dp, int idx_orig, int idx_new));
  74. static diff_T *diff_alloc_new __ARGS((diff_T *dprev, diff_T *dp));
  75.  
  76. #ifndef USE_CR
  77. # define tag_fgets vim_fgets
  78. #endif
  79.  
  80. /*
  81.  * Call this when a new buffer is being edited in the current window.  curbuf
  82.  * must already have been set.
  83.  * Marks the current buffer as being part of the diff and requireing updating.
  84.  * This must be done before any autocmd, because a command the uses info
  85.  * about the screen contents.
  86.  */
  87.     void
  88. diff_new_buffer()
  89. {
  90.     if (curwin->w_p_diff)
  91.     diff_buf_add(curbuf);
  92. }
  93.  
  94. /*
  95.  * Called when deleting or unloading a buffer: No longer make a diff with it.
  96.  * Also called when 'diff' is reset in the last window showing a diff for a
  97.  * buffer.
  98.  */
  99.     void
  100. diff_buf_delete(buf)
  101.     buf_T    *buf;
  102. {
  103.     int        i;
  104.  
  105.     i = diff_buf_idx(buf);
  106.     if (i != DB_COUNT)
  107.     {
  108.     diffbuf[i] = NULL;
  109.     diff_invalid = TRUE;
  110.     }
  111. }
  112.  
  113. /*
  114.  * Add a buffer to make diffs for.
  115.  */
  116.     void
  117. diff_buf_add(buf)
  118.     buf_T    *buf;
  119. {
  120.     int        i;
  121.  
  122.     if (diff_buf_idx(buf) != DB_COUNT)
  123.     return;        /* It's already there. */
  124.  
  125.     for (i = 0; i < DB_COUNT; ++i)
  126.     if (diffbuf[i] == NULL)
  127.     {
  128.         diffbuf[i] = buf;
  129.         diff_invalid = TRUE;
  130.         return;
  131.     }
  132.  
  133.     EMSG2(_("E96: Can not diff more than %ld buffers"), DB_COUNT);
  134. }
  135.  
  136. /*
  137.  * Find buffer "buf" in the list of diff buffers.
  138.  * Return its index or DB_COUNT if not found.
  139.  */
  140.     static int
  141. diff_buf_idx(buf)
  142.     buf_T    *buf;
  143. {
  144.     int        idx;
  145.  
  146.     for (idx = 0; idx < DB_COUNT; ++idx)
  147.     if (diffbuf[idx] == buf)
  148.         break;
  149.     return idx;
  150. }
  151.  
  152. /*
  153.  * Mark the diff info as invalid, update it when info is requested.
  154.  */
  155.     void
  156. diff_invalidate()
  157. {
  158.     if (curwin->w_p_diff)
  159.     diff_invalid = TRUE;
  160. }
  161.  
  162. /*
  163.  * Called by mark_adjust(): update line numbers.
  164.  * This attempts to update the changes as much as possible:
  165.  * When inserting/deleting lines outside of existing change blocks, create a
  166.  * new change block and update the line numbers in following blocks.
  167.  * When inserting/deleting lines in existing change blocks, update them.
  168.  */
  169.     void
  170. diff_mark_adjust(line1, line2, amount, amount_after)
  171.     linenr_T    line1;
  172.     linenr_T    line2;
  173.     long    amount;
  174.     long    amount_after;
  175. {
  176.     diff_T    *dp;
  177.     diff_T    *dprev;
  178.     diff_T    *dnext;
  179.     int        idx;
  180.     int        i;
  181.     int        inserted, deleted;
  182.     int        n, off;
  183.     linenr_T    last;
  184.     linenr_T    lnum_deleted = line1;    /* lnum of remaining deletion */
  185.     int        check_unchanged;
  186.  
  187.     /* Find the index for the current buffer. */
  188.     idx = diff_buf_idx(curbuf);
  189.     if (idx == DB_COUNT)
  190.     return;        /* This buffer doesn't have diffs. */
  191.  
  192.     if (line2 == MAXLNUM)
  193.     {
  194.     /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
  195.     inserted = amount;
  196.     deleted = 0;
  197.     }
  198.     else if (amount_after > 0)
  199.     {
  200.     /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
  201.     inserted = amount_after;
  202.     deleted = 0;
  203.     }
  204.     else
  205.     {
  206.     /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
  207.     inserted = 0;
  208.     deleted = -amount_after;
  209.     }
  210.  
  211.     dprev = NULL;
  212.     dp = first_diff;
  213.     for (;;)
  214.     {
  215.     /* If the change is after the previous diff block and before the next
  216.      * diff block, thus not touching an existing change, create a new diff
  217.      * block.  Don't do this when ex_diffgetput() is busy. */
  218.     if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
  219.             || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
  220.         && (dprev == NULL
  221.             || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
  222.         && !diff_busy)
  223.     {
  224.         dnext = diff_alloc_new(dprev, dp);
  225.         if (dnext == NULL)
  226.         return;
  227.  
  228.         dnext->df_lnum[idx] = line1;
  229.         dnext->df_count[idx] = inserted;
  230.         for (i = 0; i < DB_COUNT; ++i)
  231.         if (diffbuf[i] != NULL && i != idx)
  232.         {
  233.             if (dprev == NULL)
  234.             dnext->df_lnum[i] = line1;
  235.             else
  236.             dnext->df_lnum[i] = line1
  237.                 + (dprev->df_lnum[i] + dprev->df_count[i])
  238.                 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
  239.             dnext->df_count[i] = deleted;
  240.         }
  241.     }
  242.  
  243.     /* if at end of the list, quit */
  244.     if (dp == NULL)
  245.         break;
  246.  
  247.     /*
  248.      * Check for these situations:
  249.      *      1  2    3
  250.      *      1  2    3
  251.      * line1     2    3  4  5
  252.      *         2    3  4  5
  253.      *         2    3  4  5
  254.      * line2     2    3  4  5
  255.      *        3     5  6
  256.      *        3     5  6
  257.      */
  258.     /* compute last line of this change */
  259.     last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
  260.  
  261.     /* 1. change completely above line1: nothing to do */
  262.     if (last >= line1 - 1)
  263.     {
  264.         /* 6. change below line2: only adjust for amount_after; also when
  265.          * "deleted" became zero when deleted all lines between two diffs */
  266.         if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
  267.         {
  268.         if (amount_after == 0)
  269.             break;    /* nothing left to change */
  270.         dp->df_lnum[idx] += amount_after;
  271.         }
  272.         else
  273.         {
  274.         check_unchanged = FALSE;
  275.  
  276.         /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
  277.         if (deleted > 0)
  278.         {
  279.             if (dp->df_lnum[idx] >= line1)
  280.             {
  281.             off = dp->df_lnum[idx] - lnum_deleted;
  282.             if (last <= line2)
  283.             {
  284.                 /* 4. delete all lines of diff */
  285.                 if (dp->df_next != NULL
  286.                     && dp->df_next->df_lnum[idx] - 1 <= line2)
  287.                 {
  288.                 /* delete continues in next diff, only do
  289.                  * lines until that one */
  290.                 n = dp->df_next->df_lnum[idx] - lnum_deleted;
  291.                 deleted -= n;
  292.                 n -= dp->df_count[idx];
  293.                 lnum_deleted = dp->df_next->df_lnum[idx];
  294.                 }
  295.                 else
  296.                 n = deleted - dp->df_count[idx];
  297.                 dp->df_count[idx] = 0;
  298.             }
  299.             else
  300.             {
  301.                 /* 5. delete lines at or just before top of diff */
  302.                 n = off;
  303.                 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
  304.                 check_unchanged = TRUE;
  305.             }
  306.             dp->df_lnum[idx] = line1;
  307.             }
  308.             else
  309.             {
  310.             off = 0;
  311.             if (last < line2)
  312.             {
  313.                 /* 2. delete at end of of diff */
  314.                 dp->df_count[idx] -= last - lnum_deleted + 1;
  315.                 if (dp->df_next != NULL
  316.                     && dp->df_next->df_lnum[idx] - 1 <= line2)
  317.                 {
  318.                 /* delete continues in next diff, only do
  319.                  * lines until that one */
  320.                 n = dp->df_next->df_lnum[idx] - 1 - last;
  321.                 deleted -= dp->df_next->df_lnum[idx]
  322.                                    - lnum_deleted;
  323.                 lnum_deleted = dp->df_next->df_lnum[idx];
  324.                 }
  325.                 else
  326.                 n = line2 - last;
  327.                 check_unchanged = TRUE;
  328.             }
  329.             else
  330.             {
  331.                 /* 3. delete lines inside the diff */
  332.                 n = 0;
  333.                 dp->df_count[idx] -= deleted;
  334.             }
  335.             }
  336.  
  337.             for (i = 0; i < DB_COUNT; ++i)
  338.             if (diffbuf[i] != NULL && i != idx)
  339.             {
  340.                 dp->df_lnum[i] -= off;
  341.                 dp->df_count[i] += n;
  342.             }
  343.         }
  344.         else
  345.         {
  346.             if (dp->df_lnum[idx] <= line1)
  347.             {
  348.             /* inserted lines somewhere in this diff */
  349.             dp->df_count[idx] += inserted;
  350.             check_unchanged = TRUE;
  351.             }
  352.             else
  353.             /* inserted lines somewhere above this diff */
  354.             dp->df_lnum[idx] += inserted;
  355.         }
  356.  
  357.         if (check_unchanged)
  358.             /* Check if inserted lines are equal, may reduce the
  359.              * size of the diff.  TODO: also check for equal lines
  360.              * in the middle and perhaps split the block. */
  361.             diff_check_unchanged(dp);
  362.         }
  363.     }
  364.  
  365.     /* check if this block touches the previous one, may merge them. */
  366.     if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
  367.                               == dp->df_lnum[idx])
  368.     {
  369.         for (i = 0; i < DB_COUNT; ++i)
  370.         if (diffbuf[i] != NULL)
  371.             dprev->df_count[i] += dp->df_count[i];
  372.         dprev->df_next = dp->df_next;
  373.         vim_free(dp);
  374.         dp = dprev->df_next;
  375.     }
  376.     else
  377.     {
  378.         /* Advance to next entry. */
  379.         dprev = dp;
  380.         dp = dp->df_next;
  381.     }
  382.     }
  383.  
  384.     dprev = NULL;
  385.     dp = first_diff;
  386.     while (dp != NULL)
  387.     {
  388.     /* All counts are zero, remove this entry. */
  389.     for (i = 0; i < DB_COUNT; ++i)
  390.         if (diffbuf[i] != NULL && dp->df_count[i] != 0)
  391.         break;
  392.     if (i == DB_COUNT)
  393.     {
  394.         dnext = dp->df_next;
  395.         vim_free(dp);
  396.         dp = dnext;
  397.         if (dprev == NULL)
  398.         first_diff = dnext;
  399.         else
  400.         dprev->df_next = dnext;
  401.     }
  402.     else
  403.     {
  404.         /* Advance to next entry. */
  405.         dprev = dp;
  406.         dp = dp->df_next;
  407.     }
  408.  
  409.     }
  410.     diff_redraw(TRUE);
  411. }
  412.  
  413. /*
  414.  * Allocate a new diff block and link it between "dprev" and "dp".
  415.  */
  416.     static diff_T *
  417. diff_alloc_new(dprev, dp)
  418.     diff_T    *dprev;
  419.     diff_T    *dp;
  420. {
  421.     diff_T    *dnew;
  422.  
  423.     dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
  424.     if (dnew != NULL)
  425.     {
  426.     dnew->df_next = dp;
  427.     if (dprev == NULL)
  428.         first_diff = dnew;
  429.     else
  430.         dprev->df_next = dnew;
  431.     }
  432.     return dnew;
  433. }
  434.  
  435. /*
  436.  * Check if the diff block "dp" can be made smaller for lines at the start and
  437.  * end that are equal.  Called after inserting lines.
  438.  * This may result in a change where all buffers have zero lines, the caller
  439.  * must take care of removing it.
  440.  */
  441.     static void
  442. diff_check_unchanged(dp)
  443.     diff_T    *dp;
  444. {
  445.     int        i_org;
  446.     int        i_new;
  447.     int        off_org, off_new;
  448.     char_u    *line_org;
  449.     int        dir = FORWARD;
  450.  
  451.     /* Find the first buffers, use it as the original, compare the other
  452.      * buffer lines against this one. */
  453.     for (i_org = 0; i_org < DB_COUNT; ++i_org)
  454.     if (diffbuf[i_org] != NULL)
  455.         break;
  456.     if (i_org == DB_COUNT)    /* safety check */
  457.     return;
  458.  
  459.     if (diff_check_sanity(dp) == FAIL)
  460.     return;
  461.  
  462.     /* First check lines at the top, then at the bottom. */
  463.     off_org = 0;
  464.     off_new = 0;
  465.     for (;;)
  466.     {
  467.     /* Repeat until a line is found which is different or the number of
  468.      * lines has become zero. */
  469.     while (dp->df_count[i_org] > 0)
  470.     {
  471.         /* Copy the line, the next ml_get() will invalidate it.  */
  472.         if (dir == BACKWARD)
  473.         off_org = dp->df_count[i_org] - 1;
  474.         line_org = vim_strsave(ml_get_buf(diffbuf[i_org],
  475.                     dp->df_lnum[i_org] + off_org, FALSE));
  476.         if (line_org == NULL)
  477.         return;
  478.         for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
  479.         {
  480.         if (diffbuf[i_new] == NULL)
  481.             continue;
  482.         if (dir == BACKWARD)
  483.             off_new = dp->df_count[i_new] - 1;
  484.         /* if other buffer doesn't have this line, it was inserted */
  485.         if (off_new < 0 || off_new >= dp->df_count[i_new])
  486.             break;
  487.         if (diff_cmp(line_org, ml_get_buf(diffbuf[i_new],
  488.                    dp->df_lnum[i_new] + off_new, FALSE)) != 0)
  489.             break;
  490.         }
  491.         vim_free(line_org);
  492.  
  493.         /* Stop when a line isn't equal in all diff buffers. */
  494.         if (i_new != DB_COUNT)
  495.         break;
  496.  
  497.         /* Line matched in all buffers, remove it from the diff. */
  498.         for (i_new = i_org; i_new < DB_COUNT; ++i_new)
  499.         if (diffbuf[i_new] != NULL)
  500.         {
  501.             if (dir == FORWARD)
  502.             ++dp->df_lnum[i_new];
  503.             --dp->df_count[i_new];
  504.         }
  505.     }
  506.     if (dir == BACKWARD)
  507.         break;
  508.     dir = BACKWARD;
  509.     }
  510. }
  511.  
  512. /*
  513.  * Check if a diff block doesn't contain invalid line numbers.
  514.  * This can happen when the diff program returns invalid results.
  515.  */
  516.     static int
  517. diff_check_sanity(dp)
  518.     diff_T    *dp;
  519. {
  520.     int        i;
  521.  
  522.     for (i = 0; i < DB_COUNT; ++i)
  523.     if (diffbuf[i] != NULL)
  524.         if (dp->df_lnum[i] + dp->df_count[i] - 1
  525.                          > diffbuf[i]->b_ml.ml_line_count)
  526.         return FAIL;
  527.     return OK;
  528. }
  529.  
  530. /*
  531.  * Mark all diff buffers for redraw.
  532.  */
  533.     static void
  534. diff_redraw(dofold)
  535.     int        dofold;        /* also recompute the folds */
  536. {
  537.     win_T    *wp;
  538.  
  539.     for (wp = firstwin; wp != NULL; wp = wp->w_next)
  540.     if (wp->w_p_diff)
  541.     {
  542.         redraw_win_later(wp, NOT_VALID);
  543. #ifdef FEAT_FOLDING
  544.         if (dofold && foldmethodIsDiff(wp))
  545.         foldUpdateAll(wp);
  546. #endif
  547.     }
  548. }
  549.  
  550. /*
  551.  * Completely update the diffs for the buffers involved.
  552.  * This uses the ordinary "diff" command.
  553.  * The buffers are written to a file, also for unmodified buffers (the file
  554.  * could have been produced by autocommands, e.g. the netrw plugin).
  555.  */
  556. /*ARGSUSED*/
  557.     void
  558. ex_diffupdate(eap)
  559.     exarg_T    *eap;
  560. {
  561.     buf_T    *buf;
  562.     int        idx_orig;
  563.     int        idx_new;
  564.     char_u    *tmp_orig;
  565.     char_u    *tmp_new;
  566.     char_u    *tmp_diff;
  567.     FILE    *fd;
  568.     int        ok = FALSE;
  569.  
  570.     /* Delete all diffblocks. */
  571.     diff_clear();
  572.     diff_invalid = FALSE;
  573.  
  574.     /* Use the first buffer as the original text. */
  575.     for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
  576.     if (diffbuf[idx_orig] != NULL)
  577.         break;
  578.     if (idx_orig == DB_COUNT)
  579.     return;
  580.  
  581.     /* Only need to do something when there is another buffer. */
  582.     for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
  583.     if (diffbuf[idx_new] != NULL)
  584.         break;
  585.     if (idx_new == DB_COUNT)
  586.     return;
  587.  
  588.     /* We need three temp file names. */
  589.     tmp_orig = vim_tempname('o');
  590.     tmp_new = vim_tempname('n');
  591.     tmp_diff = vim_tempname('d');
  592.     if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL)
  593.     goto theend;
  594.  
  595.     /*
  596.      * Do a quick test if "diff" really works.  Otherwise it looks like there
  597.      * are no differences.  Can't use the return value, it's non-zero when
  598.      * there are differences.
  599.      * May try twice, first with "-a" and then without.
  600.      */
  601.     for (;;)
  602.     {
  603.     fd = fopen((char *)tmp_orig, "w");
  604.     if (fd != NULL)
  605.     {
  606.         fwrite("line1\n", (size_t)6, (size_t)1, fd);
  607.         fclose(fd);
  608.         fd = fopen((char *)tmp_new, "w");
  609.         if (fd != NULL)
  610.         {
  611.         fwrite("line2\n", (size_t)6, (size_t)1, fd);
  612.         fclose(fd);
  613.         diff_file(tmp_orig, tmp_new, tmp_diff);
  614.         fd = fopen((char *)tmp_diff, "r");
  615.         if (fd != NULL)
  616.         {
  617.             char_u    linebuf[LBUFLEN];
  618.  
  619.             for (;;)
  620.             {
  621.             /* There must be a line that contains "1c1". */
  622.             if (tag_fgets(linebuf, LBUFLEN, fd))
  623.                 break;
  624.             if (STRNCMP(linebuf, "1c1", 3) == 0)
  625.                 ok = TRUE;
  626.             }
  627.             fclose(fd);
  628.         }
  629.         mch_remove(tmp_diff);
  630.         mch_remove(tmp_new);
  631.         }
  632.         mch_remove(tmp_orig);
  633.     }
  634.  
  635.     /* If we checked if "-a" works already, break here. */
  636.     if (diff_a_works != MAYBE
  637. #ifdef FEAT_EVAL
  638.         || *p_dex != NUL
  639. #endif
  640.         )
  641.         break;
  642.     diff_a_works = ok;
  643.     if (ok)
  644.         break;
  645.     }
  646.     if (!ok)
  647.     {
  648.     EMSG(_("E97: Cannot create diffs"));
  649.     diff_a_works = MAYBE;
  650.     goto theend;
  651.     }
  652.  
  653.     /* Write the first buffer to a tempfile. */
  654.     buf = diffbuf[idx_orig];
  655.     if (buf_write(buf, tmp_orig, NULL, (linenr_T)1, buf->b_ml.ml_line_count,
  656.                      NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
  657.     goto theend;
  658.  
  659.     /* Make a difference between the first buffer and every other. */
  660.     for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
  661.     {
  662.     buf = diffbuf[idx_new];
  663.     if (buf == NULL)
  664.         continue;
  665.     if (buf_write(buf, tmp_new, NULL, (linenr_T)1, buf->b_ml.ml_line_count,
  666.                      NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
  667.         continue;
  668.     diff_file(tmp_orig, tmp_new, tmp_diff);
  669.  
  670.     /* Read the diff output and add each entry to the diff list. */
  671.     diff_read(idx_orig, idx_new, tmp_diff);
  672.     mch_remove(tmp_diff);
  673.     mch_remove(tmp_new);
  674.     }
  675.     mch_remove(tmp_orig);
  676.  
  677.     diff_redraw(TRUE);
  678.  
  679. theend:
  680.     vim_free(tmp_orig);
  681.     vim_free(tmp_new);
  682.     vim_free(tmp_diff);
  683. }
  684.  
  685. /*
  686.  * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
  687.  */
  688.     static void
  689. diff_file(tmp_orig, tmp_new, tmp_diff)
  690.     char_u    *tmp_orig;
  691.     char_u    *tmp_new;
  692.     char_u    *tmp_diff;
  693. {
  694.     char_u    *cmd;
  695.  
  696. #ifdef FEAT_EVAL
  697.     if (*p_dex != NUL)
  698.     /* Use 'diffexpr' to generate the diff file. */
  699.     eval_diff(tmp_orig, tmp_new, tmp_diff);
  700.     else
  701. #endif
  702.     {
  703.     cmd = alloc((unsigned)(STRLEN(tmp_orig) + STRLEN(tmp_new)
  704.                 + STRLEN(tmp_diff) + STRLEN(p_srr) + 17));
  705.     if (cmd != NULL)
  706.     {
  707.         /* Build the diff command and execute it.  Always use -a, binary
  708.          * differences are of no use.  Ignore errors, diff returns
  709.          * non-zero when differences have been found. */
  710.         sprintf((char *)cmd, "diff %s%s%s%s %s",
  711.             diff_a_works == FALSE ? "" : "-a ",
  712.             (diff_flags & DIFF_IWHITE) ? "-b " : "",
  713.             (diff_flags & DIFF_ICASE) ? "-i " : "",
  714.             tmp_orig, tmp_new);
  715.         append_redir(cmd, p_srr, tmp_diff);
  716.         (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
  717.         vim_free(cmd);
  718.     }
  719.     }
  720. }
  721.  
  722. /*
  723.  * Create a new version of a file from the current buffer and a diff file.
  724.  * The buffer is written to a file, also for unmodified buffers (the file
  725.  * could have been produced by autocommands, e.g. the netrw plugin).
  726.  */
  727.     void
  728. ex_diffpatch(eap)
  729.     exarg_T    *eap;
  730. {
  731.     char_u    *tmp_orig;    /* name of original temp file */
  732.     char_u    *tmp_new;    /* name of patched temp file */
  733.     char_u    *buf = NULL;
  734.     win_T    *old_curwin = curwin;
  735.     char_u    *newname = NULL;    /* name of patched file buffer */
  736. #ifdef UNIX
  737.     char_u    dirbuf[MAXPATHL];
  738.     char_u    *fullname = NULL;
  739. #endif
  740. #ifdef FEAT_BROWSE
  741.     char_u    *browseFile = NULL;
  742. #endif
  743.  
  744. #ifdef FEAT_BROWSE
  745.     if (cmdmod.browse)
  746.     {
  747.     browseFile = do_browse(TRUE, (char_u *)_("Patch file"),
  748.         NULL, NULL, eap->arg, BROWSE_FILTER_ALL_FILES, curbuf);
  749.     if (browseFile == NULL)
  750.         return;        /* operation cancelled */
  751.     eap->arg = browseFile;
  752.     }
  753. #endif
  754.  
  755.     /* We need two temp file names. */
  756.     tmp_orig = vim_tempname('o');
  757.     tmp_new = vim_tempname('n');
  758.     if (tmp_orig == NULL || tmp_new == NULL)
  759.     goto theend;
  760.  
  761.     /* Write the current buffer to "tmp_orig". */
  762.     if (buf_write(curbuf, tmp_orig, NULL,
  763.         (linenr_T)1, curbuf->b_ml.ml_line_count,
  764.                      NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
  765.     goto theend;
  766.  
  767. #ifdef UNIX
  768.     /* Get the absolute path of the patchfile, changing directory below. */
  769.     fullname = FullName_save(eap->arg, FALSE);
  770. #endif
  771.     buf = alloc((unsigned)(STRLEN(tmp_orig) + (
  772. # ifdef UNIX
  773.             fullname != NULL ? STRLEN(fullname) :
  774. # endif
  775.             STRLEN(eap->arg)) + STRLEN(tmp_new) + 16));
  776.     if (buf == NULL)
  777.     goto theend;
  778.  
  779. #ifdef UNIX
  780.     /* Temporaraly chdir to /tmp, to avoid patching files in the current
  781.      * directory when the patch file contains more than one patch.  When we
  782.      * have our own temp dir use that instead, it will be cleaned up when we
  783.      * exit (any .rej files created). */
  784.     if (mch_dirname(dirbuf, MAXPATHL) != OK)
  785.     dirbuf[0] = NUL;
  786.     else
  787.     {
  788. # ifdef TEMPDIRNAMES
  789.     if (vim_tempdir != NULL)
  790.         mch_chdir((char *)vim_tempdir);
  791.     else
  792. # endif
  793.         mch_chdir("/tmp");
  794.     shorten_fnames(TRUE);
  795.     }
  796. #endif
  797.  
  798. #ifdef FEAT_EVAL
  799.     if (*p_pex != NUL)
  800.     /* Use 'patchexpr' to generate the new file. */
  801.     eval_patch(tmp_orig,
  802. # ifdef UNIX
  803.         fullname != NULL ? fullname :
  804. # endif
  805.         eap->arg, tmp_new);
  806.     else
  807. #endif
  808.     {
  809.     /* Build the patch command and execute it.  Ignore errors.  Switch to
  810.      * cooked mode to allow the user to respond to prompts. */
  811.     sprintf((char *)buf, "patch -o %s %s < \"%s\"", tmp_new, tmp_orig,
  812. # ifdef UNIX
  813.         fullname != NULL ? fullname :
  814. # endif
  815.         eap->arg);
  816.     (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
  817.     }
  818.  
  819. #ifdef UNIX
  820.     if (dirbuf[0] != NUL)
  821.     {
  822.     mch_chdir((char *)dirbuf);
  823.     shorten_fnames(TRUE);
  824.     }
  825. #endif
  826.  
  827.     /* patch probably has written over the screen */
  828.     redraw_later(CLEAR);
  829.  
  830.     /* Delete any .orig or .rej file created. */
  831.     STRCPY(buf, tmp_new);
  832.     STRCAT(buf, ".orig");
  833.     mch_remove(buf);
  834.     STRCPY(buf, tmp_new);
  835.     STRCAT(buf, ".rej");
  836.     mch_remove(buf);
  837.  
  838.     if (curbuf->b_fname != NULL)
  839.     {
  840.     newname = vim_strnsave(curbuf->b_fname,
  841.                       (int)(STRLEN(curbuf->b_fname) + 4));
  842.     if (newname != NULL)
  843.         STRCAT(newname, ".new");
  844.     }
  845.  
  846. #ifdef FEAT_GUI
  847.     need_mouse_correct = TRUE;
  848. #endif
  849.     if (win_split(0, 0) != FAIL)
  850.     {
  851.     /* Pretend it was a ":split fname" command */
  852.     eap->cmdidx = CMD_split;
  853.     eap->arg = tmp_new;
  854.     do_exedit(eap, old_curwin);
  855.  
  856.     if (curwin != old_curwin)        /* split must have worked */
  857.     {
  858.         /* Set 'diff', 'scrollbind' on and 'wrap' off. */
  859.         diff_win_options(curwin, TRUE);
  860.         diff_win_options(old_curwin, TRUE);
  861.  
  862.         if (newname != NULL)
  863.         {
  864.         /* do a ":file filename.new" on the patched buffer */
  865.         eap->arg = newname;
  866.         ex_file(eap);
  867.  
  868. #ifdef FEAT_AUTOCMD
  869.         /* Do filetype detection with the new name. */
  870.         do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
  871. #endif
  872.         }
  873.     }
  874.     }
  875.  
  876. theend:
  877.     if (tmp_orig != NULL)
  878.     mch_remove(tmp_orig);
  879.     vim_free(tmp_orig);
  880.     if (tmp_new != NULL)
  881.     mch_remove(tmp_new);
  882.     vim_free(tmp_new);
  883.     vim_free(newname);
  884.     vim_free(buf);
  885. #ifdef UNIX
  886.     vim_free(fullname);
  887. #endif
  888. #ifdef FEAT_BROWSE
  889.     vim_free(browseFile);
  890. #endif
  891. }
  892.  
  893. /*
  894.  * Split the window and edit another file, setting options to show the diffs.
  895.  */
  896.     void
  897. ex_diffsplit(eap)
  898.     exarg_T    *eap;
  899. {
  900.     win_T    *old_curwin = curwin;
  901.  
  902. #ifdef FEAT_GUI
  903.     need_mouse_correct = TRUE;
  904. #endif
  905.     if (win_split(0, 0) != FAIL)
  906.     {
  907.     /* Pretend it was a ":split fname" command */
  908.     eap->cmdidx = CMD_split;
  909.     do_exedit(eap, old_curwin);
  910.  
  911.     if (curwin != old_curwin)        /* split must have worked */
  912.     {
  913.         /* Set 'diff', 'scrollbind' on and 'wrap' off. */
  914.         diff_win_options(curwin, TRUE);
  915.         diff_win_options(old_curwin, TRUE);
  916.     }
  917.     }
  918. }
  919.  
  920. /*
  921.  * Set options to show difs for the current window.
  922.  */
  923. /*ARGSUSED*/
  924.     void
  925. ex_diffthis(eap)
  926.     exarg_T    *eap;
  927. {
  928.     /* Set 'diff', 'scrollbind' on and 'wrap' off. */
  929.     diff_win_options(curwin, TRUE);
  930. }
  931.  
  932. /*
  933.  * Set options in window "wp" for diff mode.
  934.  */
  935.     void
  936. diff_win_options(wp, addbuf)
  937.     win_T    *wp;
  938.     int        addbuf;        /* Add buffer to diff. */
  939. {
  940.     wp->w_p_diff = TRUE;
  941.     wp->w_p_scb = TRUE;
  942.     wp->w_p_wrap = FALSE;
  943. # ifdef FEAT_FOLDING
  944.     {
  945.     win_T        *old_curwin = curwin;
  946.  
  947.     curwin = wp;
  948.     curbuf = curwin->w_buffer;
  949.     set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
  950.                                    OPT_LOCAL);
  951.     curwin = old_curwin;
  952.     curbuf = curwin->w_buffer;
  953.     wp->w_p_fdc = 2;
  954.     wp->w_p_fen = TRUE;
  955.     wp->w_p_fdl = 0;
  956.     foldUpdateAll(wp);
  957.     changed_window_setting(); /* make sure topline is not halfway a fold */
  958.     }
  959. # endif
  960.     if (addbuf)
  961.     diff_buf_add(wp->w_buffer);
  962.     redraw_win_later(wp, NOT_VALID);
  963. }
  964.  
  965. /*
  966.  * Read the diff output and add each entry to the diff list.
  967.  */
  968.     static void
  969. diff_read(idx_orig, idx_new, fname)
  970.     int        idx_orig;    /* idx of original file */
  971.     int        idx_new;    /* idx of new file */
  972.     char_u    *fname;        /* name of diff output file */
  973. {
  974.     FILE    *fd;
  975.     diff_T    *dprev = NULL;
  976.     diff_T    *dp = first_diff;
  977.     diff_T    *dn, *dpl;
  978.     long    f1, l1, f2, l2;
  979.     char_u    linebuf[LBUFLEN];   /* only need to hold the diff line */
  980.     int        difftype;
  981.     char_u    *p;
  982.     long    off;
  983.     int        i;
  984.     linenr_T    lnum_orig, lnum_new;
  985.     long    count_orig, count_new;
  986.     int        notset = TRUE;        /* block "*dp" not set yet */
  987.  
  988.     fd = fopen((char *)fname, "r");
  989.     if (fd == NULL)
  990.     {
  991.     EMSG(_("E98: Cannot read diff output"));
  992.     return;
  993.     }
  994.  
  995.     for (;;)
  996.     {
  997.     if (tag_fgets(linebuf, LBUFLEN, fd))
  998.         break;        /* end of file */
  999.     if (!isdigit(*linebuf))
  1000.         continue;        /* not the start of a diff block */
  1001.  
  1002.     /* This line must be one of three formats:
  1003.      * {first}[,{last}]c{first}[,{last}]
  1004.      * {first}a{first}[,{last}]
  1005.      * {first}[,{last}]d{first}
  1006.      */
  1007.     p = linebuf;
  1008.     f1 = getdigits(&p);
  1009.     if (*p == ',')
  1010.     {
  1011.         ++p;
  1012.         l1 = getdigits(&p);
  1013.     }
  1014.     else
  1015.         l1 = f1;
  1016.     if (*p != 'a' && *p != 'c' && *p != 'd')
  1017.         continue;        /* invalid diff format */
  1018.     difftype = *p++;
  1019.     f2 = getdigits(&p);
  1020.     if (*p == ',')
  1021.     {
  1022.         ++p;
  1023.         l2 = getdigits(&p);
  1024.     }
  1025.     else
  1026.         l2 = f2;
  1027.     if (l1 < f1 || l2 < f2)
  1028.         continue;        /* invalid line range */
  1029.  
  1030.     if (difftype == 'a')
  1031.     {
  1032.         lnum_orig = f1 + 1;
  1033.         count_orig = 0;
  1034.     }
  1035.     else
  1036.     {
  1037.         lnum_orig = f1;
  1038.         count_orig = l1 - f1 + 1;
  1039.     }
  1040.     if (difftype == 'd')
  1041.     {
  1042.         lnum_new = f2 + 1;
  1043.         count_new = 0;
  1044.     }
  1045.     else
  1046.     {
  1047.         lnum_new = f2;
  1048.         count_new = l2 - f2 + 1;
  1049.     }
  1050.  
  1051.     /* Go over blocks before the change, for which orig and new are equal.
  1052.      * Copy blocks from orig to new. */
  1053.     while (dp != NULL
  1054.         && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
  1055.     {
  1056.         if (notset)
  1057.         diff_copy_entry(dprev, dp, idx_orig, idx_new);
  1058.         dprev = dp;
  1059.         dp = dp->df_next;
  1060.         notset = TRUE;
  1061.     }
  1062.  
  1063.     if (dp != NULL
  1064.         && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
  1065.         && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
  1066.     {
  1067.         /* New block overlaps with existing block(s).
  1068.          * First find last block that overlaps. */
  1069.         for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
  1070.         if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
  1071.             break;
  1072.  
  1073.         /* If the newly found block starts before the old one, set the
  1074.          * start back a number of lines. */
  1075.         off = dp->df_lnum[idx_orig] - lnum_orig;
  1076.         if (off > 0)
  1077.         {
  1078.         for (i = idx_orig; i < idx_new; ++i)
  1079.             if (diffbuf[i] != NULL)
  1080.             dp->df_lnum[i] -= off;
  1081.         dp->df_lnum[idx_new] = lnum_new;
  1082.         dp->df_count[idx_new] = count_new;
  1083.         }
  1084.         else if (notset)
  1085.         {
  1086.         /* new block inside existing one, adjust new block */
  1087.         dp->df_lnum[idx_new] = lnum_new + off;
  1088.         dp->df_count[idx_new] = count_new - off;
  1089.         }
  1090.         else
  1091.         /* second overlap of new block with existing block */
  1092.         dp->df_count[idx_new] += count_new - count_orig;
  1093.  
  1094.         /* Adjust the size of the block to include all the lines to the
  1095.          * end of the existing block or the new diff, whatever ends last. */
  1096.         off = (lnum_orig + count_orig)
  1097.              - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
  1098.         if (off < 0)
  1099.         {
  1100.         /* new change ends in existing block, adjust the end if not
  1101.          * done already */
  1102.         if (notset)
  1103.             dp->df_count[idx_new] += -off;
  1104.         off = 0;
  1105.         }
  1106.         for (i = idx_orig; i < idx_new + !notset; ++i)
  1107.         if (diffbuf[i] != NULL)
  1108.             dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
  1109.                                - dp->df_lnum[i] + off;
  1110.  
  1111.         /* Delete the diff blocks that have been merged into one. */
  1112.         dn = dp->df_next;
  1113.         dp->df_next = dpl->df_next;
  1114.         while (dn != dp->df_next)
  1115.         {
  1116.         dpl = dn->df_next;
  1117.         vim_free(dn);
  1118.         dn = dpl;
  1119.         }
  1120.     }
  1121.     else
  1122.     {
  1123.         /* Allocate a new diffblock. */
  1124.         dp = diff_alloc_new(dprev, dp);
  1125.         if (dp == NULL)
  1126.         return;
  1127.  
  1128.         dp->df_lnum[idx_orig] = lnum_orig;
  1129.         dp->df_count[idx_orig] = count_orig;
  1130.         dp->df_lnum[idx_new] = lnum_new;
  1131.         dp->df_count[idx_new] = count_new;
  1132.  
  1133.         /* Set values for other buffers, these must be equal to the
  1134.          * original buffer, otherwise there would have been a change
  1135.          * already. */
  1136.         for (i = idx_orig + 1; i < idx_new; ++i)
  1137.         if (diffbuf[i] != NULL)
  1138.             diff_copy_entry(dprev, dp, idx_orig, i);
  1139.     }
  1140.     notset = FALSE;        /* "*dp" has been set */
  1141.     }
  1142.  
  1143.     /* for remaining diff blocks orig and new are equal */
  1144.     while (dp != NULL)
  1145.     {
  1146.     if (notset)
  1147.         diff_copy_entry(dprev, dp, idx_orig, idx_new);
  1148.     dprev = dp;
  1149.     dp = dp->df_next;
  1150.     notset = TRUE;
  1151.     }
  1152.  
  1153.     fclose(fd);
  1154. }
  1155.  
  1156. /*
  1157.  * Copy an entry at "dp" from "idx_orig" to "idx_new".
  1158.  */
  1159.     static void
  1160. diff_copy_entry(dprev, dp, idx_orig, idx_new)
  1161.     diff_T    *dprev;
  1162.     diff_T    *dp;
  1163.     int        idx_orig;
  1164.     int        idx_new;
  1165. {
  1166.     long    off;
  1167.  
  1168.     if (dprev == NULL)
  1169.     off = 0;
  1170.     else
  1171.     off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
  1172.         - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
  1173.     dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
  1174.     dp->df_count[idx_new] = dp->df_count[idx_orig];
  1175. }
  1176.  
  1177. /*
  1178.  * Clear the list of diffblocks.
  1179.  */
  1180.     static void
  1181. diff_clear()
  1182. {
  1183.     diff_T    *p, *next_p;
  1184.  
  1185.     for (p = first_diff; p != NULL; p = next_p)
  1186.     {
  1187.     next_p = p->df_next;
  1188.     vim_free(p);
  1189.     }
  1190.     first_diff = NULL;
  1191. }
  1192.  
  1193. /*
  1194.  * Check diff status for line "lnum" in buffer "buf":
  1195.  * Returns 0 for nothing special
  1196.  * Returns -1 for a line that should be highlighted as changed.
  1197.  * Returns -2 for a line that should be highlighted as added/deleted.
  1198.  * Returns > 0 for inserting that many filler lines above it (never happens
  1199.  * when 'diffopt' doesn't contain "filler").
  1200.  * This should only be used for windows where 'diff' is set.
  1201.  */
  1202.     int
  1203. diff_check(wp, lnum)
  1204.     win_T    *wp;
  1205.     linenr_T    lnum;
  1206. {
  1207.     int        idx;        /* index in diffbuf[] for this buffer */
  1208.     diff_T    *dp;
  1209.     int        maxcount;
  1210.     int        i;
  1211.     buf_T    *buf = wp->w_buffer;
  1212.     int        cmp;
  1213.  
  1214.     if (diff_invalid)
  1215.     ex_diffupdate(NULL);        /* update after a big change */
  1216.  
  1217.     if (first_diff == NULL || !wp->w_p_diff)    /* no diffs at all */
  1218.     return 0;
  1219.  
  1220.     /* safety check: "lnum" must be a buffer line */
  1221.     if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
  1222.     return 0;
  1223.  
  1224.     idx = diff_buf_idx(buf);
  1225.     if (idx == DB_COUNT)
  1226.     return 0;        /* no diffs for buffer "buf" */
  1227.  
  1228. #ifdef FEAT_FOLDING
  1229.     /* A closed fold never has filler lines. */
  1230.     if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
  1231.     return 0;
  1232. #endif
  1233.  
  1234.     /* search for a change that includes "lnum" in the list of diffblocks. */
  1235.     for (dp = first_diff; dp != NULL; dp = dp->df_next)
  1236.     if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
  1237.         break;
  1238.     if (dp == NULL || lnum < dp->df_lnum[idx])
  1239.     return 0;
  1240.  
  1241.     if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
  1242.     {
  1243.     int    zero = FALSE;
  1244.  
  1245.     /* Changed or inserted line.  If the other buffers have a count of
  1246.      * zero, the lines were inserted.  If the other buffers have the same
  1247.      * count, check if the lines are identical. */
  1248.     cmp = FALSE;
  1249.     for (i = 0; i < DB_COUNT; ++i)
  1250.         if (i != idx && diffbuf[i] != NULL)
  1251.         {
  1252.         if (dp->df_count[i] == 0)
  1253.             zero = TRUE;
  1254.         else
  1255.         {
  1256.             if (dp->df_count[i] != dp->df_count[idx])
  1257.             return -1;        /* nr of lines changed. */
  1258.             cmp = TRUE;
  1259.         }
  1260.         }
  1261.     if (cmp)
  1262.     {
  1263.         /* Compare all lines.  If they are equal the lines were inserted
  1264.          * in some buffers, deleted in others, but not changed. */
  1265.         for (i = 0; i < DB_COUNT; ++i)
  1266.         if (i != idx && diffbuf[i] != NULL && dp->df_count[i] != 0)
  1267.             if (!diff_equal_entry(dp, idx, i))
  1268.             return -1;
  1269.     }
  1270.     /* If there is no buffer with zero lines then there is no difference
  1271.      * any longer.  Happens when making a change (or undo) that removes
  1272.      * the difference.  Can't remove the entry here, we might be halfway
  1273.      * updating the window.  Just report the text as unchanged.  Other
  1274.      * windows might still show the change though. */
  1275.     if (zero == FALSE)
  1276.         return 0;
  1277.     return -2;
  1278.     }
  1279.  
  1280.     /* If 'diffopt' doesn't contain "filler", return 0. */
  1281.     if (!(diff_flags & DIFF_FILLER))
  1282.     return 0;
  1283.  
  1284.     /* Insert filler lines above the line just below the change.  Will return
  1285.      * 0 when this buf had the max count. */
  1286.     maxcount = 0;
  1287.     for (i = 0; i < DB_COUNT; ++i)
  1288.     if (diffbuf[i] != NULL && dp->df_count[i] > maxcount)
  1289.         maxcount = dp->df_count[i];
  1290.     return maxcount - dp->df_count[idx];
  1291. }
  1292.  
  1293. /*
  1294.  * Compare two entries in diff "*dp" and return TRUE if they are equal.
  1295.  */
  1296.     static int
  1297. diff_equal_entry(dp, idx1, idx2)
  1298.     diff_T    *dp;
  1299.     int        idx1;
  1300.     int        idx2;
  1301. {
  1302.     int        i;
  1303.     char_u    *line;
  1304.     int        cmp;
  1305.  
  1306.     if (dp->df_count[idx1] != dp->df_count[idx2])
  1307.     return FALSE;
  1308.     if (diff_check_sanity(dp) == FAIL)
  1309.     return FALSE;
  1310.     for (i = 0; i < dp->df_count[idx1]; ++i)
  1311.     {
  1312.     line = vim_strsave(ml_get_buf(diffbuf[idx1],
  1313.                            dp->df_lnum[idx1] + i, FALSE));
  1314.     if (line == NULL)
  1315.         return FALSE;
  1316.     cmp = diff_cmp(line, ml_get_buf(diffbuf[idx2],
  1317.                            dp->df_lnum[idx2] + i, FALSE));
  1318.     vim_free(line);
  1319.     if (cmp != 0)
  1320.         return FALSE;
  1321.     }
  1322.     return TRUE;
  1323. }
  1324.  
  1325. /*
  1326.  * Compare strings "s1" and "s2" according to 'diffopt'.
  1327.  * Return non-zero when they are different.
  1328.  */
  1329.     static int
  1330. diff_cmp(s1, s2)
  1331.     char_u    *s1;
  1332.     char_u    *s2;
  1333. {
  1334.     char_u    *p1, *p2;
  1335. #ifdef FEAT_MBYTE
  1336.     int        l;
  1337. #endif
  1338.  
  1339.     if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
  1340.     return STRCMP(s1, s2);
  1341.     if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
  1342.     return MB_STRICMP(s1, s2);
  1343.  
  1344.     /* Ignore case AND ignore white space changes. */
  1345.     p1 = s1;
  1346.     p2 = s2;
  1347.     while (*p1 != NUL && *p2 != NUL)
  1348.     {
  1349.     if (vim_iswhite(*p1) && vim_iswhite(*p2))
  1350.     {
  1351.         p1 = skipwhite(p1);
  1352.         p2 = skipwhite(p2);
  1353.     }
  1354.     else
  1355.     {
  1356. #ifdef FEAT_MBYTE
  1357.         l  = (*mb_ptr2len_check)(p1);
  1358.         if (l != (*mb_ptr2len_check)(p2))
  1359.         break;
  1360.         if (l > 1)
  1361.         {
  1362.         if (STRNCMP(p1, p2, l) != 0
  1363.             && (!enc_utf8 || utf_fold(utf_ptr2char(p1))
  1364.                            != utf_fold(utf_ptr2char(p2))))
  1365.             break;
  1366.         p1 += l;
  1367.         p2 += l;
  1368.         }
  1369.         else
  1370. #endif
  1371.         {
  1372.         if (*p1 != *p2 && TO_LOWER(*p1) != TO_LOWER(*p2))
  1373.             break;
  1374.         ++p1;
  1375.         ++p2;
  1376.         }
  1377.     }
  1378.     }
  1379.  
  1380.     /* Ignore trailing white space. */
  1381.     p1 = skipwhite(p1);
  1382.     p2 = skipwhite(p2);
  1383.     if (*p1 != NUL || *p2 != NUL)
  1384.     return 1;
  1385.     return 0;
  1386. }
  1387.  
  1388. /*
  1389.  * Return the number of filler lines above "lnum".
  1390.  */
  1391.     int
  1392. diff_check_fill(wp, lnum)
  1393.     win_T    *wp;
  1394.     linenr_T    lnum;
  1395. {
  1396.     int        n;
  1397.  
  1398.     /* be quick when there are no filler lines */
  1399.     if (!(diff_flags & DIFF_FILLER))
  1400.     return 0;
  1401.     n = diff_check(wp, lnum);
  1402.     if (n <= 0)
  1403.     return 0;
  1404.     return n;
  1405. }
  1406.  
  1407. /*
  1408.  * Set the topline of "towin" to match the position in "fromwin", so that they
  1409.  * show the same diff'ed lines.
  1410.  */
  1411.     void
  1412. diff_set_topline(fromwin, towin)
  1413.     win_T    *fromwin;
  1414.     win_T    *towin;
  1415. {
  1416.     buf_T    *buf = fromwin->w_buffer;
  1417.     linenr_T    lnum = fromwin->w_topline;
  1418.     int        idx;
  1419.     diff_T    *dp;
  1420.     int        i;
  1421.  
  1422.     idx = diff_buf_idx(buf);
  1423.     if (idx == DB_COUNT)
  1424.     return;        /* safety check */
  1425.  
  1426.     if (diff_invalid)
  1427.     ex_diffupdate(NULL);        /* update after a big change */
  1428.  
  1429.     towin->w_topfill = 0;
  1430.  
  1431.     /* search for a change that includes "lnum" in the list of diffblocks. */
  1432.     for (dp = first_diff; dp != NULL; dp = dp->df_next)
  1433.     if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
  1434.         break;
  1435.     if (dp == NULL)
  1436.     {
  1437.     /* After last change, compute topline relative to end of file; no
  1438.      * filler lines. */
  1439.     towin->w_topline = towin->w_buffer->b_ml.ml_line_count
  1440.                        - (buf->b_ml.ml_line_count - lnum);
  1441.     }
  1442.     else
  1443.     {
  1444.     /* Find index for "towin". */
  1445.     i = diff_buf_idx(towin->w_buffer);
  1446.     if (i == DB_COUNT)
  1447.         return;        /* safety check */
  1448.  
  1449.     towin->w_topline = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
  1450.     if (lnum >= dp->df_lnum[idx])
  1451.     {
  1452.         /* Inside a change: compute filler lines. */
  1453.         if (dp->df_count[i] == dp->df_count[idx])
  1454.         towin->w_topfill = fromwin->w_topfill;
  1455.         else if (dp->df_count[i] > dp->df_count[idx])
  1456.         {
  1457.         if (lnum == dp->df_lnum[idx] + dp->df_count[idx])
  1458.             towin->w_topline = dp->df_lnum[i] + dp->df_count[i]
  1459.                              - fromwin->w_topfill;
  1460.         }
  1461.         else
  1462.         {
  1463.         if (towin->w_topline >= dp->df_lnum[i] + dp->df_count[i])
  1464.         {
  1465.             if (diff_flags & DIFF_FILLER)
  1466.             towin->w_topfill = dp->df_lnum[idx]
  1467.                            + dp->df_count[idx] - lnum;
  1468.             towin->w_topline = dp->df_lnum[i] + dp->df_count[i];
  1469.         }
  1470.         }
  1471.     }
  1472.     }
  1473.  
  1474.     /* safety check (if diff info gets outdated strange things may happen) */
  1475.     towin->w_botfill = FALSE;
  1476.     if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
  1477.     {
  1478.     towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
  1479.     towin->w_botfill = TRUE;
  1480.     }
  1481.     if (towin->w_topline < 1)
  1482.     {
  1483.     towin->w_topline = 1;
  1484.     towin->w_topfill = 0;
  1485.     }
  1486.     check_topfill(towin, FALSE);
  1487. #ifdef FEAT_FOLDING
  1488.     (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
  1489.                                 NULL, TRUE, NULL);
  1490. #endif
  1491. }
  1492.  
  1493. /*
  1494.  * This is called when 'diffopt' is changed.
  1495.  */
  1496.     int
  1497. diffopt_changed()
  1498. {
  1499.     char_u    *p;
  1500.     int        diff_context_new = 6;
  1501.     int        diff_flags_new = 0;
  1502.  
  1503.     p = p_dip;
  1504.     while (*p != NUL)
  1505.     {
  1506.     if (STRNCMP(p, "filler", 6) == 0)
  1507.     {
  1508.         p += 6;
  1509.         diff_flags_new |= DIFF_FILLER;
  1510.     }
  1511.     else if (STRNCMP(p, "context:", 8) == 0 && isdigit(p[8]))
  1512.     {
  1513.         p += 8;
  1514.         diff_context_new = getdigits(&p);
  1515.     }
  1516.     else if (STRNCMP(p, "icase", 5) == 0)
  1517.     {
  1518.         p += 5;
  1519.         diff_flags_new |= DIFF_ICASE;
  1520.     }
  1521.     else if (STRNCMP(p, "iwhite", 6) == 0)
  1522.     {
  1523.         p += 6;
  1524.         diff_flags_new |= DIFF_IWHITE;
  1525.     }
  1526.     if (*p != ',' && *p != NUL)
  1527.         return FAIL;
  1528.     if (*p == ',')
  1529.         ++p;
  1530.     }
  1531.  
  1532.     /* If "icase" or "iwhite" was added or removed, need to update the diff. */
  1533.     if (diff_flags != diff_flags_new)
  1534.     diff_invalid = TRUE;
  1535.  
  1536.     diff_flags = diff_flags_new;
  1537.     diff_context = diff_context_new;
  1538.  
  1539.     diff_redraw(TRUE);
  1540.  
  1541.     /* recompute the scroll binding with the new option value, may
  1542.      * remove or add filler lines */
  1543.     check_scrollbind((linenr_T)0, 0L);
  1544.  
  1545.     return OK;
  1546. }
  1547.  
  1548. /*
  1549.  * Find the difference within a changed line.
  1550.  * Returns TRUE if the line was added, no other buffer has it.
  1551.  */
  1552.     int
  1553. diff_find_change(wp, lnum, startp, endp)
  1554.     win_T    *wp;
  1555.     linenr_T    lnum;
  1556.     int        *startp;    /* first char of the change */
  1557.     int        *endp;        /* last char of the change */
  1558. {
  1559.     char_u    *line_org;
  1560.     char_u    *line_new;
  1561.     int        i;
  1562.     int        si, ei_org, ei_new;
  1563.     diff_T    *dp;
  1564.     int        idx;
  1565.     int        off;
  1566.     int        added = TRUE;
  1567.  
  1568.     /* Make a copy of the line, the next ml_get() will invalidate it. */
  1569.     line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
  1570.     if (line_org == NULL)
  1571.     return FALSE;
  1572.  
  1573.     idx = diff_buf_idx(wp->w_buffer);
  1574.     if (idx == DB_COUNT)    /* cannot happen */
  1575.     return FALSE;
  1576.  
  1577.     /* search for a change that includes "lnum" in the list of diffblocks. */
  1578.     for (dp = first_diff; dp != NULL; dp = dp->df_next)
  1579.     if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
  1580.         break;
  1581.     if (dp == NULL || diff_check_sanity(dp) == FAIL)
  1582.     return FALSE;
  1583.  
  1584.     off = lnum - dp->df_lnum[idx];
  1585.  
  1586.     for (i = 0; i < DB_COUNT; ++i)
  1587.     if (diffbuf[i] != NULL && i != idx)
  1588.     {
  1589.         /* Skip lines that are not in the other change (filler lines). */
  1590.         if (off >= dp->df_count[i])
  1591.         continue;
  1592.         added = FALSE;
  1593.         line_new = ml_get_buf(diffbuf[i], dp->df_lnum[i] + off, FALSE);
  1594.  
  1595.         /* Search for start of difference */
  1596.         for (si = 0; line_org[si] != NUL && line_org[si] == line_new[si]; )
  1597.         ++si;
  1598.         if (*startp > si)
  1599.         *startp = si;
  1600.  
  1601.         /* Search for end of difference, if any. */
  1602.         if (line_org[si] != NUL || line_new[si] != NUL)
  1603.         {
  1604.         ei_org = (int)STRLEN(line_org);
  1605.         ei_new = (int)STRLEN(line_new);
  1606.         while (ei_org >= *startp && ei_new >= *startp
  1607.             && ei_org >= 0 && ei_new >= 0
  1608.             && line_org[ei_org] == line_new[ei_new])
  1609.         {
  1610.             --ei_org;
  1611.             --ei_new;
  1612.         }
  1613.         if (*endp < ei_org)
  1614.             *endp = ei_org;
  1615.         }
  1616.     }
  1617.  
  1618.     vim_free(line_org);
  1619.     return added;
  1620. }
  1621.  
  1622. #if defined(FEAT_FOLDING) || defined(PROTO)
  1623. /*
  1624.  * Return TRUE if line "lnum" is not close to a diff block, this line should
  1625.  * be in a fold.
  1626.  * Return FALSE if there are no diff blocks at all in this window.
  1627.  */
  1628.     int
  1629. diff_infold(wp, lnum)
  1630.     win_T    *wp;
  1631.     linenr_T    lnum;
  1632. {
  1633.     int        i;
  1634.     int        idx = -1;
  1635.     int        other = FALSE;
  1636.     diff_T    *dp;
  1637.  
  1638.     /* Return if 'diff' isn't set. */
  1639.     if (!wp->w_p_diff)
  1640.     return FALSE;
  1641.  
  1642.     for (i = 0; i < DB_COUNT; ++i)
  1643.     {
  1644.     if (diffbuf[i] == wp->w_buffer)
  1645.         idx = i;
  1646.     else if (diffbuf[i] != NULL)
  1647.         other = TRUE;
  1648.     }
  1649.  
  1650.     /* return here if there are no diffs in the window */
  1651.     if (idx == -1 || !other)
  1652.     return FALSE;
  1653.  
  1654.     if (diff_invalid)
  1655.     ex_diffupdate(NULL);        /* update after a big change */
  1656.  
  1657.     /* Return if there are no diff blocks.  All lines will be folded. */
  1658.     if (first_diff == NULL)
  1659.     return TRUE;
  1660.  
  1661.     for (dp = first_diff; dp != NULL; dp = dp->df_next)
  1662.     {
  1663.     /* If this change is below the line there can't be any further match. */
  1664.     if (dp->df_lnum[idx] - diff_context > lnum)
  1665.         break;
  1666.     /* If this change ends before the line we have a match. */
  1667.     if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
  1668.         return FALSE;
  1669.     }
  1670.     return TRUE;
  1671. }
  1672. #endif
  1673.  
  1674. /*
  1675.  * ":diffget"
  1676.  * ":diffput"
  1677.  */
  1678.     void
  1679. ex_diffgetput(eap)
  1680.     exarg_T    *eap;
  1681. {
  1682.     linenr_T    lnum;
  1683.     int        count;
  1684.     linenr_T    off = 0;
  1685.     diff_T    *dp;
  1686.     diff_T    *dprev;
  1687.     diff_T    *dfree;
  1688.     int        idx_cur;
  1689.     int        idx_other;
  1690.     int        idx_from;
  1691.     int        idx_to;
  1692.     int        i;
  1693.     int        added;
  1694.     char_u    *p;
  1695.     aco_save_T    aco;
  1696.     buf_T    *buf;
  1697.     int        start_skip, end_skip;
  1698.     int        new_count;
  1699.  
  1700.     /* Find the current buffer in the list of diff buffers. */
  1701.     idx_cur = diff_buf_idx(curbuf);
  1702.     if (idx_cur == DB_COUNT)
  1703.     {
  1704.     EMSG(_("E99: Current buffer is not in diff mode"));
  1705.     return;
  1706.     }
  1707.  
  1708.     if (*eap->arg == NUL)
  1709.     {
  1710.     /* No argument: Find the other buffer in the list of diff buffers. */
  1711.     for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
  1712.         if (diffbuf[idx_other] != curbuf && diffbuf[idx_other] != NULL)
  1713.         break;
  1714.     if (idx_other == DB_COUNT)
  1715.     {
  1716.         EMSG(_("E100: No other buffer in diff mode"));
  1717.         return;
  1718.     }
  1719.  
  1720.     /* Check that there isn't a third buffer in the list */
  1721.     for (i = idx_other + 1; i < DB_COUNT; ++i)
  1722.         if (diffbuf[i] != curbuf && diffbuf[i] != NULL)
  1723.         {
  1724.         EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
  1725.         return;
  1726.         }
  1727.     }
  1728.     else
  1729.     {
  1730.     /* Buffer number or pattern given.  Ignore trailing white space. */
  1731.     p = eap->arg + STRLEN(eap->arg);
  1732.     while (p > eap->arg && vim_iswhite(p[-1]))
  1733.         --p;
  1734.     for (i = 0; isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
  1735.         ;
  1736.     if (eap->arg + i == p)        /* digits only */
  1737.         i = atol((char *)eap->arg);
  1738.     else
  1739.     {
  1740.         i = buflist_findpat(eap->arg, p, FALSE, TRUE);
  1741.         if (i < 0)
  1742.         return;        /* error message already given */
  1743.     }
  1744.     buf = buflist_findnr(i);
  1745.     if (buf == NULL)
  1746.     {
  1747.         EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
  1748.         return;
  1749.     }
  1750.     idx_other = diff_buf_idx(buf);
  1751.     if (idx_other == DB_COUNT)
  1752.     {
  1753.         EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
  1754.         return;
  1755.     }
  1756.     }
  1757.  
  1758.     diff_busy = TRUE;
  1759.  
  1760.     /* When no range given include the line above the cursor. */
  1761.     if (eap->addr_count == 0 && eap->line1 > 1)
  1762.     --eap->line1;
  1763.  
  1764.     if (eap->cmdidx == CMD_diffget)
  1765.     {
  1766.     idx_from = idx_other;
  1767.     idx_to = idx_cur;
  1768.     }
  1769.     else
  1770.     {
  1771.     idx_from = idx_cur;
  1772.     idx_to = idx_other;
  1773.     /* Need to make the other buffer the current buffer to be able to make
  1774.      * changes in it. */
  1775.     /* set curwin/curbuf to buf and save a few things */
  1776.     aucmd_prepbuf(&aco, diffbuf[idx_other]);
  1777.     }
  1778.  
  1779.     dprev = NULL;
  1780.     for (dp = first_diff; dp != NULL; )
  1781.     {
  1782.     if (dp->df_lnum[idx_cur] > eap->line2 + off)
  1783.         break;    /* past the range that was specified */
  1784.  
  1785.     dfree = NULL;
  1786.     lnum = dp->df_lnum[idx_to];
  1787.     count = dp->df_count[idx_to];
  1788.     if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
  1789.         && u_save(lnum - 1, lnum + count) != FAIL)
  1790.     {
  1791.         /* Inside the specified range and saving for undo worked. */
  1792.         start_skip = 0;
  1793.         end_skip = 0;
  1794.         if (eap->addr_count > 0)
  1795.         {
  1796.         /* A range was specified: check if lines need to be skipped. */
  1797.         start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
  1798.         if (start_skip > 0)
  1799.         {
  1800.             /* range starts below start of current diff block */
  1801.             if (start_skip > count)
  1802.             {
  1803.             lnum += count;
  1804.             count = 0;
  1805.             }
  1806.             else
  1807.             {
  1808.             count -= start_skip;
  1809.             lnum += start_skip;
  1810.             }
  1811.         }
  1812.         else
  1813.             start_skip = 0;
  1814.  
  1815.         end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
  1816.                              - (eap->line2 + off);
  1817.         if (end_skip > 0)
  1818.         {
  1819.             /* range ends above end of current/from diff block */
  1820.             if (idx_cur == idx_from)    /* :diffput */
  1821.             {
  1822.             i = dp->df_count[idx_cur] - start_skip - end_skip;
  1823.             if (count > i)
  1824.                 count = i;
  1825.             }
  1826.             else            /* :diffget */
  1827.             {
  1828.             count -= end_skip;
  1829.             end_skip = dp->df_count[idx_from] - start_skip - count;
  1830.             if (end_skip < 0)
  1831.                 end_skip = 0;
  1832.             }
  1833.         }
  1834.         else
  1835.             end_skip = 0;
  1836.         }
  1837.  
  1838.         added = 0;
  1839.         for (i = 0; i < count; ++i)
  1840.         {
  1841.         ml_delete(lnum, FALSE);
  1842.         --added;
  1843.         }
  1844.         for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
  1845.         {
  1846.         linenr_T nr;
  1847.  
  1848.         nr = dp->df_lnum[idx_from] + start_skip + i;
  1849.         if (nr > diffbuf[idx_from]->b_ml.ml_line_count)
  1850.             break;
  1851.         p = vim_strsave(ml_get_buf(diffbuf[idx_from], nr, FALSE));
  1852.         ml_append(lnum + i - 1, p, 0, FALSE);
  1853.         ++added;
  1854.         }
  1855.         new_count = dp->df_count[idx_to] + added;
  1856.         dp->df_count[idx_to] = new_count;
  1857.  
  1858.         if (start_skip == 0 && end_skip == 0)
  1859.         {
  1860.         /* Check if there are any other buffers and if the diff is
  1861.          * equal in them. */
  1862.         for (i = 0; i < DB_COUNT; ++i)
  1863.             if (diffbuf[i] != NULL && i != idx_from && i != idx_to
  1864.                 && !diff_equal_entry(dp, idx_from, i))
  1865.             break;
  1866.         if (i == DB_COUNT)
  1867.         {
  1868.             /* delete the diff entry, the buffers are now equal here */
  1869.             dfree = dp;
  1870.             dp = dp->df_next;
  1871.             if (dprev == NULL)
  1872.             first_diff = dp;
  1873.             else
  1874.             dprev->df_next = dp;
  1875.         }
  1876.         }
  1877.  
  1878.         /* Adjust marks.  This will change the following entries! */
  1879.         if (added != 0)
  1880.         {
  1881.         mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
  1882.         if (curwin->w_cursor.lnum >= lnum)
  1883.         {
  1884.             /* Adjust the cursor position if it's in/after the changed
  1885.              * lines. */
  1886.             if (curwin->w_cursor.lnum >= lnum + count)
  1887.             curwin->w_cursor.lnum += added;
  1888.             else if (added < 0)
  1889.             curwin->w_cursor.lnum = lnum;
  1890.         }
  1891.         }
  1892.         changed_lines(lnum, 0, lnum + count, (long)added);
  1893.  
  1894.         if (dfree != NULL)
  1895.         {
  1896.         /* Diff is deleted, update folds in other windows. */
  1897. #ifdef FEAT_FOLDING
  1898.         diff_fold_update(dfree, idx_to);
  1899. #endif
  1900.         vim_free(dfree);
  1901.         }
  1902.         else
  1903.         /* mark_adjust() may have changed the count in a wrong way */
  1904.         dp->df_count[idx_to] = new_count;
  1905.  
  1906.         /* When changing the current buffer, keep track of line numbers */
  1907.         if (idx_cur == idx_to)
  1908.         off += added;
  1909.     }
  1910.  
  1911.     /* If before the range or not deleted, go to next diff. */
  1912.     if (dfree == NULL)
  1913.     {
  1914.         dprev = dp;
  1915.         dp = dp->df_next;
  1916.     }
  1917.     }
  1918.  
  1919.     /* restore curwin/curbuf and a few other things */
  1920.     if (idx_other == idx_to)
  1921.     {
  1922.     /* Syncing undo only works for the current buffer, but we change
  1923.      * another buffer.  Sync undo if the command was typed.  This isn't
  1924.      * 100% right when ":diffput" is used in a function or mapping. */
  1925.     if (KeyTyped)
  1926.         u_sync();
  1927.     aucmd_restbuf(&aco);
  1928.     }
  1929.  
  1930.     diff_busy = FALSE;
  1931.  
  1932.     /* Check that the cursor is on a valid character and update it's position.
  1933.      * When there were filler lines the topline has become invalid. */
  1934.     check_cursor();
  1935.     changed_line_abv_curs();
  1936.  
  1937.     /* Also need to redraw the other buffers. */
  1938.     diff_redraw(FALSE);
  1939. }
  1940.  
  1941. #ifdef FEAT_FOLDING
  1942. /*
  1943.  * Update folds for all diff buffers for entry "dp".
  1944.  * Skip buffer with index "skip_idx".
  1945.  * When there are no diffs, all folds are removed.
  1946.  */
  1947.     static void
  1948. diff_fold_update(dp, skip_idx)
  1949.     diff_T    *dp;
  1950.     int        skip_idx;
  1951. {
  1952.     int        i;
  1953.     win_T    *wp;
  1954.  
  1955.     for (wp = firstwin; wp != NULL; wp = wp->w_next)
  1956.     for (i = 0; i < DB_COUNT; ++i)
  1957.         if (diffbuf[i] == wp->w_buffer && i != skip_idx)
  1958.         foldUpdate(wp, dp->df_lnum[i],
  1959.                         dp->df_lnum[i] + dp->df_count[i]);
  1960. }
  1961. #endif
  1962.  
  1963. /*
  1964.  * Return TRUE if buffer "buf" is in diff-mode.
  1965.  */
  1966.     int
  1967. diff_mode_buf(buf)
  1968.     buf_T    *buf;
  1969. {
  1970.     return diff_buf_idx(buf) != DB_COUNT;
  1971. }
  1972.  
  1973. /*
  1974.  * Move "count" times in direction "dir" to the next diff block.
  1975.  * Return FAIL if there isn't such a diff block.
  1976.  */
  1977.     int
  1978. diff_move_to(dir, count)
  1979.     int        dir;
  1980.     long    count;
  1981. {
  1982.     int        idx;
  1983.     linenr_T    lnum = curwin->w_cursor.lnum;
  1984.     diff_T    *dp;
  1985.  
  1986.     idx = diff_buf_idx(curbuf);
  1987.     if (idx == DB_COUNT || first_diff == NULL)
  1988.     return FAIL;
  1989.  
  1990.     if (diff_invalid)
  1991.     ex_diffupdate(NULL);        /* update after a big change */
  1992.  
  1993.     if (first_diff == NULL)        /* no diffs today */
  1994.     return FAIL;
  1995.  
  1996.     while (--count >= 0)
  1997.     {
  1998.     /* Check if already before first diff. */
  1999.     if (dir == BACKWARD && lnum <= first_diff->df_lnum[idx])
  2000.         break;
  2001.  
  2002.     for (dp = first_diff; ; dp = dp->df_next)
  2003.     {
  2004.         if (dp == NULL)
  2005.         break;
  2006.         if ((dir == FORWARD && lnum < dp->df_lnum[idx])
  2007.             || (dir == BACKWARD
  2008.             && (dp->df_next == NULL
  2009.                 || lnum <= dp->df_next->df_lnum[idx])))
  2010.         {
  2011.         lnum = dp->df_lnum[idx];
  2012.         break;
  2013.         }
  2014.     }
  2015.     }
  2016.  
  2017.     /* don't end up past the end of the file */
  2018.     if (lnum > curbuf->b_ml.ml_line_count)
  2019.     lnum = curbuf->b_ml.ml_line_count;
  2020.  
  2021.     /* When the cursor didn't move at all we fail. */
  2022.     if (lnum == curwin->w_cursor.lnum)
  2023.     return FAIL;
  2024.  
  2025.     setpcmark();
  2026.     curwin->w_cursor.lnum = lnum;
  2027.     curwin->w_cursor.col = 0;
  2028.  
  2029.     return OK;
  2030. }
  2031. #endif    /* FEAT_DIFF */
  2032.